import { createRouter, createWebHistory } from 'vue-router';

// 假设你已经创建了各个组件
import GoodsList from '@/components/GoodsList.vue';
import SalesOrders from '@/components/SalesOrders.vue';
import CustomerList from '@/components/CustomerList.vue';
import AddGood from '@/components/AddGood.vue';
import UserRegister from '@/components/UserRegister.vue';
import UserLogin from '@/components/UserLogin.vue';
import InventoryList from '@/components/InventoryList.vue';
import AddInventoryItem from '@/components/AddInventoryItem.vue';
import SalesOrderList from '@/components/SalesOrderList.vue';
import AddSalesOrder from '@/components/AddSalesOrder.vue';
import AddCustomer from '@/components/AddCustomer.vue';
import EditGood from '@/components/EditGood.vue';
import StockManagement from '@/components/StockManagement.vue';
import UserManagement from '@/components/UserManagement.vue';
import ReportManagement from '@/components/ReportManagement.vue';
import HomePage from '@/components/HomePage.vue';
import WarehouseOverview from '@/components/WarehouseOverview.vue';
import SettingsList from '@/components/SettingsList.vue';
import ReportList from '@/components/ReportList.vue';
import InventoryAlerts from '@/components/InventoryAlerts.vue';


// 创建路由器实例
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes: [
    { path: '/', redirect: '/home' },
    { path: '/home', component: HomePage },
    { path: '/goods', component: GoodsList },
    { path: '/sales-orders', component: SalesOrders },
    { path: '/goods/add', component: AddGood },
    { path: '/register', component: UserRegister },
    { path: '/login', component: UserLogin },
    { path: '/inventory', component: InventoryList },
    { path: '/inventory/add', component: AddInventoryItem },
    { path: '/sales/orders', component: SalesOrderList },
    { path: '/sales/orders/add', component: AddSalesOrder },
    { path: '/customers/add', component: AddCustomer },
    { path: '/goods/edit/:id', name: 'EditGood', component: EditGood },
    { path: '/stock-management', component: StockManagement },
    { path: '/user-management', component: UserManagement },
    { path: '/report-management', component: ReportManagement },
    { path: '/warehouse-overview', component: WarehouseOverview },
    { path: '/customers', component: CustomerList },
    { path: '/report', component: ReportList },
    { path: '/settings', component: SettingsList },
    { path: '/inventory-alerts', component: InventoryAlerts}
  ]
});

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next();
  } else {
    const token = localStorage.getItem('access');
    if (token) {
      const isTokenExpired = checkTokenExpiration(token);
      if (isTokenExpired) {
        localStorage.removeItem('access');
        next('/login');
      } else {
        next();
      }
    } else {
      next('/login');
    }
  }
});

// 检查 token 是否过期的函数
function checkTokenExpiration(token) {
  try {
    const base64Url = token.split('.')[1];
    const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
    const decodedToken = JSON.parse(atob(base64));
    const expiresAt = decodedToken.exp; // JWT 的过期时间戳
    const currentTime = Math.floor(Date.now() / 1000); // 当前时间戳
    return expiresAt < currentTime;
  } catch (error) {
    console.error('Error decoding token:', error);
    return true; // 如果解析失败，假设 token 已过期
  }
}

// 添加路由守卫以在每次导航时刷新页面
router.beforeEach((to, from, next) => {
  if (to.path === from.path) {
    // 如果目标路径与当前路径相同，强制刷新页面
    window.location.reload();
  } else {
    // 否则正常导航
    next();
  }
});

export default router;